
import java.util.*;
import java.net.*;
import java.text.*;


/**
 * A simple read-only container for a single "hit" on a search query.
 */

class SearchHit 
{
    private final SearchWorker worker;
    private URL url;
    private String title;
    private String text;
    private Date date;


    private static String getMapString(Map m, String k) {
	String v = (String)(m.get(k));
	return (v != null) ? v.trim() : null;
    }


    public SearchHit(SearchWorker worker, Map m) {
	super();
	this.worker = worker;

	String urlString = getMapString(m, "url");
	title = getMapString(m, "title");
	text = getMapString(m, "text");

	try {
	    if (!(urlString.startsWith("http://"))) {
		urlString = "http://" + urlString;
	    }
	    url = new URL(urlString);
	}
	catch (MalformedURLException e) {
	    url = null;
	}
    }


    public SearchWorker getWorker() {
	return worker;
    }

    public URL getURL() {
	return url;
    }

    public Date getDate() {
	return date;
    }

    public String getTitle() {
	return title;
    }

    public String getText() {
	return text;
    }


    public String toString() {
	URL url = getURL();
	Date date = getDate();
	String time = (date != null) ? DateFormat.getDateInstance().format(date) : "";
	String host = (url != null) ? url.getHost() : "<couldn't parse URL>";
	return getWorker() + " " + time + " " + host + " " + getTitle();
    }

}
